package com.xudow.app.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.wor.WrongTopicWithInfo;
import com.daimajia.swipe.SwipeLayout;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.util.ImageUtils;
import com.xudow.app.util.MessageUtils;

import java.util.List;

/**
 * 错题列表Adapter
 *
 * @author lukecheng
 */
public class MistakeListAdapter extends ArrayAdapter<WrongTopicWithInfo> {

    public interface ItemListener {
        void onClick(int position);

        void onDelete(int position);
    }

    private ItemListener itemListener;

    public void setItemListener(ItemListener itemListener) {
        this.itemListener = itemListener;
    }

    private List<WrongTopicWithInfo> mistakes;
    private Context context;

    public MistakeListAdapter(Context context, int resource, List<WrongTopicWithInfo> mistakes) {
        super(context, resource, mistakes);

        this.context = context;
        this.mistakes = mistakes;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (null == convertView) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_class_mistake, null);

            holder = new ViewHolder();
            holder.root = (SwipeLayout) convertView.findViewById(R.id.root);
            holder.detailItem = (LinearLayout) convertView.findViewById(R.id.detail_item);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.comment = (TextView) convertView.findViewById(R.id.comment);
            holder.image = (ImageView) convertView.findViewById(R.id.image);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        WrongTopicWithInfo wtf = mistakes.get(position);

        String time = MessageUtils.formatTimeStampString(context, wtf.getCreateDate());

        holder.title.setText(wtf.getTitle());
        holder.comment.setText(wtf.getRemark());
        holder.time.setText(time);

        String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, wtf.getWrongImageUrl());
        ImageUtils.loadImage(this.getContext(), holder.image, imageUrl, R.mipmap.course_def_img);

        holder.detailItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != itemListener) {
                    itemListener.onClick(position);
                }
            }
        });

        holder.root.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout swipeLayout) {

            }

            @Override
            public void onOpen(SwipeLayout swipeLayout) {
                LinearLayout bottomWrapper = (LinearLayout) swipeLayout.findViewById(R.id.bottom_wrapper);
                LinearLayout deleteItem = (LinearLayout) swipeLayout.findViewById(R.id.delete_item);
                ImageView deleteImage = (ImageView) swipeLayout.findViewById(R.id.delete_image);
                TextView deleteText = (TextView) swipeLayout.findViewById(R.id.delete_text);

                View.OnClickListener listener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != itemListener) {
                            itemListener.onDelete(position);
                        }
                    }
                };

                bottomWrapper.setOnClickListener(listener);
                deleteItem.setOnClickListener(listener);
                deleteImage.setOnClickListener(listener);
                deleteText.setOnClickListener(listener);
            }

            @Override
            public void onStartClose(SwipeLayout swipeLayout) {

            }

            @Override
            public void onClose(SwipeLayout swipeLayout) {

            }

            @Override
            public void onUpdate(SwipeLayout swipeLayout, int i, int i1) {

            }

            @Override
            public void onHandRelease(SwipeLayout swipeLayout, float v, float v1) {

            }
        });

        return convertView;
    }

    class ViewHolder {
        public SwipeLayout root;
        private LinearLayout detailItem;
        private LinearLayout deleteItem;
        public TextView title, time, comment;
        public ImageView image;
    }
}
